<?php

namespace App\Http\Middleware;

use Closure;
use Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate;

class WechatOAuthAuthenticate extends OAuthAuthenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $account = 'default', $scopes = null)
    {
        if ($this->isWeChatBrowser($request)) {
            return parent::handle($request, $next, $account, $scopes);
        }
        return $next($request);
    }

    /**
     * Detect current user agent type.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return bool
     */
    protected function isWeChatBrowser($request)
    {
        return false !== strpos($request->header('user_agent', ''), 'MicroMessenger');
    }
}
